package com.itheima.web.servlet;

import com.itheima.domain.Cart;
import com.itheima.domain.CartItem;
import com.itheima.domain.Route;
import com.itheima.domain.User;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RouteService;
import com.itheima.util.CartUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;

@WebServlet("/CartServlet")
public class CartServlet extends BaseServlet {
    private RouteService routeService = (RouteService) BeanFactory.getBean("routeService");

    //作为模板使用
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    //删除购物车选项
    protected void delCartItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求参数
        String rid = request.getParameter("rid");
        //2.从session获取user对象
        User currentUser = (User) request.getSession().getAttribute("currentUser");
       //3.从redis中获取购物车
        Cart cart = CartUtils.getCartForRedis(currentUser);
        //4.根据rid删除购物项
        cart.getCartItemMap().remove(rid);
        //5.将cart更新到redis中
        CartUtils.setCartToRedis(currentUser,cart);
           //6.重定向查看看购物车
        response.sendRedirect(request.getContextPath()+"/CartServlet?action=findAll");

    }
    // 查看购物车
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.从session中获取user
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        // 2.从redis中获取cart
        Cart cart = CartUtils.getCartForRedis(currentUser);
        // 3.设置到requsetr域
        request.setAttribute("cart", cart);
        // 4.转发到cart.jsp
        request.getRequestDispatcher("/cart.jsp").forward(request, response);
    }

    //1.添加购物车
    protected void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接受请求参数
        String num = request.getParameter("num");
        String rid = request.getParameter("rid");
        //2.从redis中获取购物车
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        Cart cart = CartUtils.getCartForRedis(currentUser);
        //3.判断此商品在购物车中是否存在
        LinkedHashMap<String, CartItem> cartItemMap = cart.getCartItemMap();
        CartItem cartItem = cartItemMap.get(rid);
        if (cartItem != null) {//存在
            //数量增加
            cartItem.setNum(cartItem.getNum() + Integer.parseInt(num));

        } else {//不存在
            // 根据rid查询route对象
            Route route = routeService.findDetail(rid);
            // 创建购物项对象
            cartItem=new CartItem();
            cartItem.setNum(Integer.parseInt(num));
            cartItem.setRoute(route);
            // 设置到购物车中
            cartItemMap.put(rid,cartItem);
        }
        // 4.将购物车更新到redis中
        CartUtils.setCartToRedis(currentUser,cart);
        // 5.将购物项设置到request域，展示给用
        request.setAttribute("cartItem",cartItem);
        // 6.转发到添加成功页面
        request.getRequestDispatcher("/cart_success.jsp").forward(request,response);
    }

}
